package spring.cloud.study.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import spring.cloud.study.entity.base.RespCodeEnum;
import spring.cloud.study.entity.base.ResponseModel;
import spring.cloud.study.feign.ConsumerFeignClient;
import spring.cloud.study.hystrix.HystrixService;

@RestController
public class MovieController {

	private static final Logger logger = LoggerFactory.getLogger(MovieController.class);

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private ConsumerFeignClient feignClient;

	@Autowired
	private HystrixService hystrixService;

	@GetMapping("/ribbon")
	public @ResponseBody ResponseModel getByRest() {
		try {
			return restTemplate.getForObject("http://EUREKA-PROVIDER/provider/hello",ResponseModel.class);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseModel.errorExceptionMsg(e.getMessage());
		}
	}

	@GetMapping("/consumerFeign")
	public ResponseModel consumerFeign() {
		return feignClient.hello();
	}

	@GetMapping("/hystrix")
	public @ResponseBody ResponseModel hystrix() {
		try {
			return hystrixService.doInvoke();
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseModel.errorExceptionMsg(e.getMessage());
		}
	}

	@GetMapping("/hystrixLimit")
	public @ResponseBody ResponseModel hystrixLimit() {
		try {
			return hystrixService.hystrixLimit();
		} catch (Exception e) {
			return ResponseModel.errorMsg(RespCodeEnum.EXCEPTION.parseError(e.getMessage()));
		}
	}
	

}
